<template>
    <div class="test">
        <!-- <x-header title="新增日常检查" @header-background-color=""></x-header>-->
        <div class="form-section">
            <div class="form-item form-item-style1">
                <div class="label-title">
                    <label>被检查单位</label>
                    <i class="iconfont icon-required"></i>
                </div>
                <div class="right-select"
                     v-if="userInfo.departsir==1">
                    <p>
                        {{$store.state.checkcompanyNew.Name?$store.state.checkcompanyNew.Name:"请选择"}}
                    </p>
                </div>

                <div class="right-select"
                     v-if="userInfo.departsir!=1"
                     @click="selectType">
                    <p>
                        {{$store.state.checkcompanyNew.Name?$store.state.checkcompanyNew.Name:"请选择"}}
                    </p>
                    <i class="iconfont icon-jinru rightmsg"></i>
                </div>
                <!--<group>
                    <popup-picker :data="list3"
                                  :columns="3"
                                  placeholder='请选择'
                                  v-model="value4"
                                  show-name></popup-picker>
                </group>-->
            </div>
            <div class="form-item form-item-style1">
                <div class="label-title">
                    <label>检查类型</label>
                    <i class="iconfont icon-required"></i>
                </div>
                <div class="right-select"
                     @click="selectNew">
                    <p>{{dayneed}}</p>
                </div>
            </div>
            <div class="form-item form-item-style1">
                <div class="label-title">
                    <label>检查时间</label>
                    <i class="iconfont icon-required"></i>
                </div>
                <group>
                    <!--:start-date="'2019-09-09'"-->
                    <datetime :start-date="showDate"
                              v-model="$store.state.checktimeNew"
                              @on-change="changetime"></datetime>
                </group>
            </div>
        </div>

        <div class="form-section">
            <div class="form-item form-item-style1">
                <div class="label-title">
                    <label class="lookmsg">检查记录
                        <!--<span>（90分）</span>-->
                    </label>
                </div>
                <div class="right-select"
                     @click="lookdj">
                    <p>登记&nbsp;&nbsp;</p>
                    <i class="iconfont icon-zengjia"></i>
                </div>
            </div>
        </div>
        <div class="form-section">
            <div v-for="(item,index) in menulist"
                 :key="index">
                <template>
                    <div class="form-item form-item-style1 looktest">
                        <div class="label-title">
                            <label class="lookmsgTwo">{{item.key?item.key:"其他"}}
                                <!--<span>（90分）</span>-->
                            </label>
                        </div>
                        <group>
                            <cell is-link
                                  :border-intent="false"
                                  :arrow-direction="showarr[index]? 'up' : 'down'"
                                  @click.native="showmsg(index)">
                                <span class="selectspan">共计{{item.value.length}}条隐患</span>
                            </cell>
                        </group>
                    </div>
                    <template v-if="showarr[index]">
                        <leftmove v-for="(itemlist,indexlist) in item.value"
                                  :key="indexlist"
                                  @removemsg="removemsg(item,index,indexlist)">
                            <div class="contentlist"
                                 @click="showlistitem(index,indexlist,itemlist)">
                                <div class="card-padding">
                                    <div class="card-title1">{{itemlist.checkDeptName}}</div>
                                    <div class="card-show">{{itemlist.description}}</div>
                                    <div class="danger-nomal">【{{itemlist.hdLevel[0]}}】
                                        <span>{{itemlist.state?itemlist.state:""}}</span>
                                    </div>
                                </div>
                                <div class="newImage">
                                    <img :src="itemlist.uploadImgs[0]?itemlist.uploadImgs[0].url:''">
                                </div>
                                <!-- <div class="newImage">
                                    <img :src="itemlist.localIds[0]?itemlist.localIds[0]:''">
                                </div> -->
                            </div>
                        </leftmove>
                    </template>
                </template>
            </div>
        </div>
        <div class="form-section">
            <div class="form-item form-item-style1 ">
                <div class="label-title">
                    <label class="lookmsg">生成通知单</label>
                </div>
            </div>
            <div class="form-item form-item-style1"
                 @click="showlook(1)">
                <div class="label-title">
                    <label>生成隐患整改通知单</label>
                </div>
                <div class="right-selects">
                    <x-icon v-if="$store.state.selectidlist1New.length>0"
                            type="ios-checkmark-empty"
                            size="30"
                            class="iconclick"></x-icon>
                    <i class="iconfont icon-jinru"></i>
                </div>
            </div>
            <div class="form-item form-item-style1"
                 @click="showlook(2)">
                <div class="label-title">
                    <label>生成设备停用通知单</label>
                </div>
                <div class="right-selects">
                    <x-icon v-if="$store.state.selectidlist2New.length>0"
                            type="ios-checkmark-empty"
                            size="30"
                            class="iconclick"></x-icon>
                    <i class="iconfont icon-jinru"></i>
                </div>
            </div>
            <div class="form-item form-item-style1"
                 @click="showlook(3)">
                <div class="label-title">
                    <label>生成停工通知单</label>
                </div>
                <div class="right-selects">
                    <x-icon v-if="$store.state.selectidlist3New.length>0"
                            type="ios-checkmark-empty"
                            size="30"
                            class="iconclick"></x-icon>
                    <i class="iconfont icon-jinru"></i>
                </div>
            </div>
            <div class="btn-bottom"
                 @click="submitmsg">
                <div class="btn">提交</div>
            </div>
            <div v-transfer-dom>
                <XDialog v-model="show">
                    <div class="diago">
                        <div class="top-header">
                            你还没有生成通知单，确定要提交吗？
                        </div>
                        <div class="showdetail">
                            <div class="showbtn"
                                 @click="cancelbtn">取消</div>
                            <div class="truebtn"
                                 @click="truebtn">确定</div>
                        </div>
                    </div>
                </XDialog>
            </div>
        </div>
        <companyProp :companyPropShow="companyPropShow"
                     :selectCompany="selectCompany"
                     :selectSlef="false"></companyProp>
    </div>
</template>

<script>
import { XHeader, Tabbar, TabbarItem, Group, Cell, Card, XDialog, TransferDom, InlineLoading, Popup } from 'vux'
import leftmove from './left'
import myMenu from '@/commonComponents/menu'
import companyProp from '@/commonComponents/companyProp'
export default {
    name: 'App',
    directives: {
        TransferDom
    },
    data () {
        return {
            inputValue: '',
            showContent001: false,
            formdata: {
                conpamy: ''
            },
            dayneed: '',
            role: '',
            showarr: [],
            addfirst: '',
            menulist: [],
            baseList: '',
            hidelist: '',
            show: false,
            checktime: '',

            userId: '',
            tokenId: '',
            projectId: '',
            userInfo: '',
            showDate: '',
            companyPropShow: false // 选择单位弹框
        }
    },
    components: {
        Tabbar,
        TabbarItem,
        Group,
        Cell,
        XHeader,
        Card,
        leftmove,
        XDialog,
        myMenu,
        InlineLoading,
        Popup,
        companyProp
    },
    computed: {
        //   localStorage(){
        //       debugger;
        //   }
    },
    created () {
        // this.userId = this.$store.state.userId;
        // this.tokenId = this.$store.state.tokenId;
        // this.projectId = this.$store.state.companyId;
        this.userInfo = JSON.parse(localStorage.userInfo)
        this.userId = this.userInfo.userid
        this.tokenId = this.userInfo.tokenId
        this.projectId = this.userInfo.deptid
        this.role = this.userInfo.departsir
        if (!this.$store.state.checktimeNew) {
            this.$store.state.checktimeNew = this.getdatenew()
            this.showDate = this.getdatenew()
        }
        // this.getCompany()
    },
    activated () {
        if (!this.$store.state.checktimeNew) {
            this.$store.state.checktimeNew = this.getdatenew()
            this.showDate = this.getdatenew()
        }
        this.userInfo = JSON.parse(localStorage.userInfo)
        this.userId = this.userInfo.userid
        this.tokenId = this.userInfo.tokenId
        this.projectId = this.userInfo.deptid
        this.role = this.userInfo.departsir

        // this.$store.state.hiddenlist = ''
        this.$store.state.localId = ''
        this.dayneed = '日常检查'
        this.checktime = localStorage.checktime
        this.$forceUpdate()
        if (this.role == 1) {
            this.lookcompanylist()
        } else {
            this.baseList = this.$store.state.checkcompanyNew
        }
        let listget = []
        listget = [...this.$store.state.HiddenDangersNew]
        this.menulist = [] // 数据过滤
        listget.forEach(
            (item, index) => {
                let flag = true
                for (var indexs = 0; indexs < this.menulist.length; indexs++) {
                    if (item.category == this.menulist[indexs].key) {
                        this.menulist[indexs]['value'].push(item)
                        flag = false
                    }
                }
                if (flag) {
                    this.menulist = [...this.menulist, {
                        key: item.category,
                        value: [item]
                    }]
                }
            }
        )
    },
    methods: {
        showlook (showindex) {
            let listget = [...this.$store.state.HiddenDangersNew]
            let arr = []

            listget.forEach(
                (item) => {
                    let flag = this.typechange(item)
                    if (item.state == '已填写') {
                        arr = [...arr, item]
                    }
                }
            )

            if (arr.length > 0) {
                this.$store.state.noticeHidden = arr
                this.$store.state.type = showindex
                this.$router.push({
                    path: '/testing/notice',
                    query: {
                        type: showindex
                    }
                })
            } else {
                this.$vux.toast.text('请填写检查记录')
            }
        },
        typechange (item) {
            if (!item.checkDeptName) {
                return false
            }
            return true
        },
        selectType () {
            // this.$router.push({
            //     path: '/testing/lookcompany',
            //     query: {
            //         type: 'testing'
            //     }
            // })
            this.companyPropShow = true
        },
        lookdj () {
            if (!this.$store.state.checkcompanyNew.Name) {
                this.$vux.toast.text('请输入被检查单位')
                return false
            }
            if (!this.$store.state.checktimeNew) {
                this.$vux.toast.text('请输入检查时间')
                return false
            }
            //    this.$store.state.hiddenlist.deptName = this.$store.state.checkcompany.Name;
            //    this.$store.state.hiddenlist.deptId = this.$store.state.checkcompany.Id;

            this.$store.state.hiddenlist = Object.assign({}, this.$store.state.hiddenlist, {
                'deptName': this.$store.state.checkcompanyNew.Name,
                'deptId': this.$store.state.checkcompanyNew.Id,
                'projectDutyPerson': this.$store.state.checkcompanyNew.DutyPerson
            })
            this.$router.push({ path: '/hideRegister' })
            localStorage.newaddtwo = ''
        },
        lookrecord () {
            this.$router.push({ path: '/testing/lookdetail' })
        },
        submitmsg () {
            if (!this.$store.state.checkcompanyNew.Name) {
                this.$vux.toast.text('请输入被检查单位')
                this.show = false
                return false
            }
            if (!this.$store.state.checktimeNew) {
                this.$vux.toast.text('请输入检查时间')
                this.show = false
                return false
            }
            let listget = [...this.$store.state.HiddenDangersNew]
            if (listget.length == 0) {
                this.show = true
            } else {
                let flag = false
                listget.forEach(
                    (item) => {
                        if (item.state == '未填写') {
                            flag = true
                        }
                    }
                )
                if (flag) {
                    this.$vux.toast.text('请填写检查记录')
                    return false
                }

                if (this.$store.state.selectidlist1New.length == 0 && this.$store.state.selectidlist2New.length == 0 && this.$store.state.selectidlist3New.length == 0) {
                    this.show = true
                    return false
                }
                this.submitNametwo()
            }

            //
        },
        truebtn () {
            this.show = false
            this.submitNametwo()
        },
        selectNew () {
            this.dayneed = '日常检查'
        },
        submitName () { // 新增日常检查项目级，无隐患
            let param = {
                'allowPaging': true,
                'business': 'SubmitCheckWork',
                'data': {
                    'HiddenDangers': [],
                    'addProblemType': this.baseList.Category, // 检查级别
                    'checkDate': this.$store.state.checktimeNew, // 查询时间
                    'checkType': '日常检查',
                    'dutyPerson': this.$store.state.checkcompanyNew.DutyPerson,
                    'projectId': this.$store.state.checkcompanyNew.Id,
                    'projectName': this.$store.state.checkcompanyNew.Name
                },
                'pageIndex': 0,
                'pageSize': 20,
                'tokenId': this.tokenId,
                'userId': this.userId
            }
            // debugger

            this.$axiosAjax.getConfigJson(
                param
            ).then(
                (res) => {
                    if (res.data.code == 0) {
                        this.show = false
                        this.$vux.toast.text('新增日常检查成功')
                    }
                }
            )
        },
        submitNametwo () { // 新增日常检查项目级，无隐患
            // if(this.role==2){
            let arr = []
            let formData = new FormData()
            this.$store.state.HiddenDangersNew.forEach(
                (item, index) => {
                    item.uploadImgs.forEach(
                        (itemlist, indexlist) => {
                            //   let  y = (item['newBlobList'][indexlist].type).substring(6)
                            //  formData.append(itemlist.name, item['images'][indexlist])
                            formData.append(item['newBlobList'][indexlist].name, item['newBlobList'][indexlist])
                            // formData.append(itemlist.name, (item['formData']).get(itemlist.name))
                        }
                    )
                    arr = [...arr, {
                        'category': item.category, // 1
                        'categoryId': item.categoryId, // 1
                        'categoryIds': item.categoryId,
                        'checkDeptCode': item.checkDeptCode, // d
                        'checkDeptId': item.checkDeptId, // d
                        'checkDeptName': item.checkDeptName, // d
                        'checkPerson': item.checkPerson, // d
                        'checkPersonId': item.checkPersonId, // d
                        'checkTime': item.checkTime, // 1
                        'checkWorkId': item.checkWorkId, // 检测信息
                        'deptId': item.deptId, // 所属项目ID
                        'deptName': item.deptName, // 所属项目名称
                        'description': item.description, // 隐患描述
                        'hdLevel': item.hdLevel[0],
                        'isApproved': item.isApproved,
                        'isBig': item.hdLevel[0] == '一般隐患' ? 0 : 1,
                        'isChecked': item.isChecked,
                        'isResolved': item.isResolved,
                        'isSubmited': item.isSubmited,
                        'localId': item.localId,
                        'measure': item.measure,
                        'measureEdit': item.isChecked,
                        'no': item.no,
                        'problemImgs': item.problemImgs,
                        'projectDutyPerson': item.projectDutyPerson, // 项目负责人姓名||
                        'registerUserId': item.registerUserId, // 登陆拿
                        'resolveArea': item.resolveArea,
                        'resolveAreaDutyPerson': item.resolveAreaDutyPerson,
                        'resolveAreaDutyPersonId': item.resolveAreaDutyPersonId,
                        'resolveEndTime': item.resolveEndTime,
                        'resolveStartTime': item.resolveStartTime,
                        'tel': item.tel,
                        'userId': item.userId,
                        'ResolveDeptName': item.ResolveDeptName, // 整改单位
                        'ResolveDeptCode': item.ResolveDeptCode,
                        'ResolveDeptId': item.ResolveDeptId,
                        'ResolveDutyPersonName': item.ResolveDutyPersonName,
                        'ResolveGroupId': item.ResolveGroupId,
                        'ResolveGroup': item.ResolveGroup,
                        'ResolveGroupDutyPerson': item.ResolveGroupDutyPerson
                    }]
                }
            )
            // }
            let param = {
                'allowPaging': true,
                'business': 'SubmitCheckWork',
                'data': {
                    'HiddenDangerId1': this.$store.state.selectidlist1New.join('_'),
                    'HiddenDangerId2': this.$store.state.selectidlist2New.join('_'),
                    'HiddenDangerId3': this.$store.state.selectidlist3New.join('_'),
                    'HiddenDangers': arr,
                    'addProblemType': this.$store.state.checkcompanyNew.Category,
                    'checkDate': this.$store.state.checktimeNew,
                    'checkType': '日常检查',
                    'dutyPerson': this.$store.state.checkcompanyNew.DutyPerson,
                    'projectId': this.$store.state.checkcompanyNew.Id,
                    'projectName': this.$store.state.checkcompanyNew.Name,
                    'EquipmentId': this.$route.query.id
                },
                'pageIndex': 0,
                'pageSize': 20,
                'tokenId': this.tokenId,
                'userId': this.userId

            }

            // alert(params);
            this.$vux.loading.show({
                text: '加载中...'
            })
            this.$axiosAjax.getConfigJson(
                param, formData
            ).then(
                (res) => {
                    if (res.data.code == 0) {
                        this.$store.state.selectidlist1New = []
                        this.$store.state.selectidlist2New = []
                        this.$store.state.selectidlist3New = []
                        this.$store.state.checkcompanyNew = ''
                        this.$store.state.checktimeNew = ''
                        this.$store.state.HiddenDangersNew = []
                        this.$vux.loading.hide()
                        // this.theModel = res.data.data
                        if (this.$route.query.id) {
                            this.$router.replace('/equipmentIndex')
                        } else {
                            this.$router.push('/safeCheck')
                        }
                    }
                }
            )
        },
        lookcompanylist () { // 查看项目级公司信息
            let param = {
                'allowPaging': true,
                'business': 'getHdProjects',
                'data': this.userInfo.deptid,
                'pageIndex': 0,
                'pageSize': 20,
                'tokenId': this.tokenId,
                'userId': this.userId
            }
            this.$axiosAjax.getConfigJson(
                param
            ).then(
                (res) => {
                    if (res.data.code == 0) {
                        this.formdata = {
                            company: res.data.data[0].Name
                        }
                        this.baseList = res.data.data[0]
                        this.$store.state.checkcompanyNew = res.data.data[0]
                        localStorage.baseList = JSON.stringify(this.baseList)
                        // localStorage.mainperson = res.data.data[0].DutyPerson
                    }
                }
            )
        },
        showmsg (index) {
            this.showarr[index] = !this.showarr[index]
            this.$forceUpdate()
        },
        removemsg (item, index, indexlist) {
            let arr = this.menulist[index]['value']
            arr.splice(indexlist, 1)
            this.menulist[index]['value'] = arr
            this.$store.state.selectidlist1New.forEach(
                (newitem, newindex) => {
                    if (item.localId = newitem) {
                        this.$store.state.selectidlist1New.splice(newindex, 1)
                    }
                }
            )
            this.$store.state.selectidlist2New.forEach(
                (newitem, newindex) => {
                    if (item.localId = newitem) {
                        this.$store.state.selectidlist2New.splice(newindex, 1)
                    }
                }
            )
            this.$store.state.selectidlist3New.forEach(
                (newitem, newindex) => {
                    if (item.localId = newitem) {
                        this.$store.state.selectidlist3New.splice(newindex, 1)
                    }
                }
            )
            this.$forceUpdate()
            this.newchangelist()
        },
        newchangelist () {
            let arr = []
            this.menulist.forEach(
                (item) => {
                    if (item.value.length > 0) {
                        (item.value).forEach(
                            (items) => {
                                arr = [...arr, items]
                            }
                        )
                    }
                }
            )
            this.$store.state.HiddenDangersNew = arr
        },
        showlistitem (index, indexlist, item) {
            this.$store.state.hiddenlistLocalId[item.localId] = item
            this.$store.state.localId = item.localId
            this.$router.push({ path: '/hideRegister', query: { 'localId': item.localId } })
        },
        cancelbtn () {
            this.show = false
        },
        changetime (val) {
            this.$store.state.checktimeNew = val
            // localStorage.checktime = val;
        },
        getdatenew () {
            var date = new Date()
            var seperator1 = '-'
            var year = date.getFullYear()
            var month = date.getMonth() + 1
            var strDate = date.getDate()
            if (month >= 1 && month <= 9) {
                month = '0' + month
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = '0' + strDate
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate
            return currentdate
        },
        selectCompany (item) {
            this.companyPropShow = false
            this.$store.state.checkcompanyNew = item
        }
    }

}
</script>

<style scoped lang="less">
.test {
    min-height: 100%;
    background: #ebeef2;
}
.right-select {
    display: flex;
    justify-content: flex-end;
    font-size: 13px;
    color: #999;
    height: 20px;
    width: 180px;
    position: relative;
    padding-right: 20px;
    line-height: 20px;
}
.right-select p {
    width: 130px;
    text-align: right;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.icon-jinru {
    color: #c8c8cd;
}
.lookmsg {
    color: #2269ff;
}
.lookmsgTwo {
    color: #000000;
}
.lookmsgTwo > span {
    margin-left: 10px;
    color: #999999;
}
.lookmsg > span {
    margin-left: 10px;
    color: #999999;
}
.card-padding {
    height: 100px;
    width: 230px;
    padding-right: 30px;
}
.newImage {
    width: 100px;
    height: 74px;
    border-radius: 7px;
}
.newImage img {
    width: 100%;
    height: 100%;
}
.contentlist {
    width: 100%;
    height: 106px;
    display: flex;
    justify-content: space-between;
    padding: 16px 20px 16px 0;
    border-bottom: 1px solid #efeff4;
}
.card-title1 {
    width: 100%;
    //   height:15px;
    line-height: 15px;
    font-size: 15px;
    font-weight: bold;
    color: #333333;
    margin-top: 0;
    padding-top: 4px;
    overflow: hidden;
    text-overflow: ellipsis; /* 当对象内文本溢出时显示省略标记(...) ；需与overflow:hidden;一起使用。*/
    white-space: nowrap; /*不换行 */
}
.card-show {
    padding-top: 12px;
    width: 100%;
    height: 26px;
    line-height: 14px;
    font-size: 14px;
    color: #666666;
    overflow: hidden;
    text-overflow: ellipsis; /* 当对象内文本溢出时显示省略标记(...) ；需与overflow:hidden;一起使用。*/
    white-space: nowrap; /*不换行 */
}
.danger-nomal {
    margin-top: 20px;
    color: #ff7333;
    font-size: 14px;
}
.danger-nomal > span {
    color: #999999;
}
.btn-bottom {
    width: 100%;
    height: 115px;
    padding-top: 41px;
}
.btn {
    height: 40px;
    width: 204px;
    border-radius: 20px;
    text-align: center;
    line-height: 40px;
    color: #fff;
    background-color: #2269ff;
    margin: 0 auto;
}
.rightmsg {
    color: #c8c8cd;
    position: absolute;
    right: 0;
    top: 0;
}
.icon-zengjia {
    position: absolute;
    right: 0px;
    color: #15bd84;
    font-size: 22px;
}
.selectspan {
    margin-right: 8px;
    color: #b0b0b0;
    font-size: 14px;
}
.diago {
    padding: 16px;
}
.showdetail {
    display: flex;
    padding-top: 30px;
    justify-content: space-around;
}
.showbtn {
    width: 100px;
    height: 40px;
    border-radius: 20px;
    text-align: center;
    line-height: 40px;
    border: 1px solid #2269ff;
}
.truebtn {
    width: 100px;
    height: 40px;
    border-radius: 20px;
    text-align: center;
    line-height: 40px;
    background-color: #2269ff;
    color: #fff;
}
.iconclick {
    vertical-align: middle;
    fill: #2269ff;
}
</style>
